<template>
    <div class="index">
       <Header>
		<span>找回密码</span>
	   </Header>
		<section>
				<div class="login-tel">
					<input type="text" v-model="userTel" placeholder="请输入手机号码" pattern="[0-9]" />
				</div>
				<div class="login-code">
					<input type="text" placeholder="请输入验证码" pattern="[0-9]" v-model="userCode" />
					<button 
					:disabled="disabled"
					@click="sendCode"
					>
					{{codeMsg}}
					</button>
				</div>
				<div class="login-btn" @click="next">下 一 步</div>
				
		</section>
		<Tabbar></Tabbar>
    </div>
</template>
<script>
import  Tabbar  from "@/components/common/Tabbar.vue";
import  Header  from "@/views/login/Header.vue";
import { Toast } from 'mint-ui';
import http from '@/common/api/request.js';
export default{
	data(){
		return{
			disabled:false,
			userTel:'',
			userCode:'',
			rules:{
				//手机号验证规则
				userTel:{
					rule:/^1[23456789]\d{9}$/,
					msg:'手机号不能为空，并且是11位数字',
					
				},
			},
			codeNum:6,
			codeMsg:'获取短信验证码',
			code:'',
		}
	},
	components: {
		Tabbar,
		Header,
	},
	methods:{
		//获取短信验证码按钮
		sendCode(){
			if( !validate('userTel') ) return;
			//请求短信验证码接口
			http.$axios({
				url:'/api/code',
				method:'POST',
				data:{
					phone:this.userTel
				}
			}).then(res=>{
				if(res.success){
					this.code = res.data;
				}
				
			})

			//禁用按钮
			this.disabled = true;
			//倒计时
			let timer = setInterval(()=>{
				--this.codeNum;
				this.codeMsg = `重新发送${this.codeNum}`;
			},1000)
			//判断什么时候停止定时器
			setTimeout(()=>{
				clearInterval(timer);
				//将倒计时恢复到6并且按钮可用,文本仍然为获取短信验证码
				this.codeNum = 6;
				this.disabled = false;
				this.codeMsg = '获取短信验证码';
			},6000)
		},
		//跳转到了密码登录
		goUserLogin(){
			//验证
			this.$router.push('/userLogin');
		},
		//跳转到快捷注册
		goRegister(){
			this.$router.push('/register');
		},
		
		//验证信息提示
		validate( key ){
			let bool = true;
			if( !this.rules[key].rule.test( this[key]) ){
				//提示信息
				Toast(this.rules[key].msg);
				bool = false;
				return false;
			}
			return bool;
		},
		//点击下一步
		next(){
			//如果验证码不正确
			if( this.code != this.userCode ){
				Toast('验证码不正确！');
				return;
			}
			//告诉后端，用户输入的手机号存在吗？
				http.$axios({
					url:'/api/selectUser',
					method:'POST',
					data:{
						phone:this.userTel
					}
				}).then(res=>{
					//如果此用户不存在
					if( !res.success ){
						Toast(res.msg);
						return false;
					}
					//如果存在则跳转页面
					this.$router.push({
						name:'btn',
						query:{
							phone:this.userTel
						}
					})
				})
				
			
		},
		
	},
	
}
</script>

<style scoped>
.login{
	display: flex;
	flex-direction: column;
	width: 100vw;
	height: 100vh;
	overflow: hidden;
}
section{
	display: flex;
	flex-direction: column;
	background-color: #f5f5f5;
	align-items: center;
}
section div{
	margin: 0.625rem 0;
	width: 20.9375rem;
	height: 2.75rem;
	
}
.login-tel{
	margin-top: 1.25rem;
}
.login-tel input{
	width: 100%;
}
input{
	box-sizing: border-box;
	padding: 0 0.625rem;
	background-color: #fff;
	line-height: 2.75rem;
	border: 0.0625rem solid #ccc;
	border-radius: 0.375rem;
	
}
.login-code {
	display: flex;	
}
.login-code input{
	flex: 1;
}
.login-code button{
	padding: 0 1.25rem;
	line-height: 2.75rem;
	color: #fff;
	background-color: green;
	border: 0;
	border-radius: 0.375rem;
}
.login-btn{
	line-height: 2.75rem;
	color: #fff;
	text-align: center;
	background-color: green;
	border-radius: 0.375rem;
	
}

</style>